<?php

defined( 'IN_DICE' ) or die( 'Access Error!' );

include_once( DIR_MDL . 'platform/platform.php' );
include_once( DIR_MDL . 'platform/class.facebook.php' );

class pfb implements platform {
	
	/**
	 * @var Facebook
	 */
	public $op;
	public $pfid;
	public $apiid;
	public $apikey;
	public $apisecret;
	public $apiurl;
	
	public function __construct() {
		$this->pfid = all::$cfg['facebook_pfid'];
		$this->apiid = all::$cfg['facebook_apiid'];
		$this->apikey = all::$cfg['facebook_apikey'];
		$this->apisecret = all::$cfg['facebook_apisecret'];
		$this->apiurl = all::$cfg['facebook_apiurl'];
		$this->op = new Facebook( array( 'appId'  => $this->apiid,  'secret' => $this->apisecret,  'cookie' => true, ) );
	}
	
	/**
	 * 获取当前登录用户的平台ID.字符串型,没有获取到则为空字符串
	 */
	public function getUserId() {
		$session = $this->op->getSession();
		if( $session ) {
			$uid = $this->op->getUser();
		}
		all::$aOut['session'] = json_encode( $session );
		
		return $uid;
	}
	public function getUserInfo( $uid ) {
		if( !strlen( $uid ) ) {
			return array();
		}
		/*$params = array ( //新方法取用户资料,不支持取其它的头像,暂时还是使用老的方法取资料,等文档更新
	    			"uids"	=> array( $uid ),
	    			"fields"=> array('id','location','education','hometown','name','picture','gender','birthday'),
					);
		$uinfo = $this->op->api( "/$uid", array( 'fields' => implode( ',', $params['fields'] ) ) );*/
		
		$params = array (
					"method" => 'users.getInfo',
	    			"uids"	=> array( $uid ),
	    			"fields"=> array('uid','name','pic','sex',),
					);
		$uinfo = $this->op->api( $params );
		if( !isset( $uinfo[0] ) || empty ( $uinfo[0] ) ) {
			return array();
		}
		$uinfo = $uinfo[0]; //二维数组
		
		$aSex['man'] = 1;
		$aSex['female'] = 0;
		
		$aSex['男'] = 1;
  		$aSex['女'] = 0;
  		
  		$aSex['男性'] = 1;
  		$aSex['女性'] = 0;
  		
		return ( strcmp($uid, $uinfo['uid']) !== 0) ? array() : array( //错误则返回空数组,否则严格返回以下格式
																	'puid' => $uinfo['uid'],
																	'uname' => $uinfo['name'],
																	'headurl' => $uinfo['pic'],
																	'sex' => isset($aSex[$uinfo['sex']]) ? $aSex[$uinfo['sex']] : 1,
																	'psuid' => '',
																	'upage' => "http://www.facebook.com/profile.php?id={$uinfo['uid']}",
																	'usid' => 0, //用户的子ID,用于同城或者本站
																	'pfid' => all::$cfg['facebook_pfid'],
																	);
	}
	public function getAllFriends(){
		$list = $this->op->friends('get');
		return is_array($list['uid']) ? $list['uid'] : array();
	}
	public function getAppFriends(){
		$params = array ( "method" => 'friends.getAppUsers', );
		$aPF = $this->op->api( $params );
		return is_array($aPF) ? $aPF : array();
	}
	
	/**
	 * 应用授权，用户是否添加此应用或已登录
	 */
	public function isAdded() {
		$session = $this->op->getSession();
		$aReq = $this->op->getSignedRequest();
		$url = $this->apiurl . '?' . http_build_query( $_GET );
		if( empty( $session ) && $aReq ) { //未添加应用,跳转
			$url = $this->op->getLoginUrl( array( 'next' => $url, 'cancel_url' => $url, 'req_perms' => 'publish_stream,user_photos' ) );
			echo "<fb:redirect url='$url' />";
			func::redirect( $url, '_top', 'js' );
		}
		if( empty( $session ) ) {
			echo "<fb:redirect url='$url' />";
			func::redirect( $url, '_top', 'js' );
		}
		all::$aOut['session'] = json_encode( $session );
	}
	
	public function getSiteInfo() {
		
	}
	public function sendNotice() {
		
	}
	public function sendFeed( $aFields ) {
		
	}
	public function setProfile() {
		
	}
	public function setKey( $key ) {
		$this->op->session_key = $key;
	}

	public function getpayurl() {
		return $this->apiurl . '?m=game&a=pay';
	}
	public function isFans( $puid ) { //get whether is fb pages fans
		$params = array (
					"method" => 'pages.isfan',
	    			"page_id" => $this->apiid,
	    			"uid" => $puid,
					"callback" => '',
					);
		$fans = $this->op->api( $params );
		return $fans;
	}
}
